{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 15.14 \u4f20\u9012Unicode\u5b57\u7b26\u4e32\u7ed9C\u51fd\u6570\u5e93\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u8981\u5199\u4e00\u4e2a\u6269\u5c55\u6a21\u5757\uff0c\u9700\u8981\u5c06\u4e00\u4e2aPython\u5b57\u7b26\u4e32\u4f20\u9012\u7ed9C\u7684\u67d0\u4e2a\u5e93\u51fd\u6570\uff0c\u4f46\u662f\u8fd9\u4e2a\u51fd\u6570\u4e0d\u77e5\u9053\u8be5\u600e\u4e48\u5904\u7406Unicode\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u91cc\u6211\u4eec\u9700\u8981\u8003\u8651\u5f88\u591a\u7684\u95ee\u9898\uff0c\u4f46\u662f\u6700\u4e3b\u8981\u7684\u95ee\u9898\u662f\u73b0\u5b58\u7684C\u51fd\u6570\u5e93\u5e76\u4e0d\u7406\u89e3Python\u7684\u539f\u751fUnicode\u8868\u793a\u3002\n\u56e0\u6b64\uff0c\u4f60\u7684\u6311\u6218\u662f\u5c06Python\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u4e00\u4e2a\u80fd\u88abC\u7406\u89e3\u7684\u5f62\u5f0f\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u6f14\u793a\u7684\u76ee\u7684\uff0c\u4e0b\u9762\u6709\u4e24\u4e2aC\u51fd\u6570\uff0c\u7528\u6765\u64cd\u4f5c\u5b57\u7b26\u4e32\u6570\u636e\u5e76\u8f93\u51fa\u5b83\u6765\u8c03\u8bd5\u548c\u6d4b\u8bd5\u3002\n\u4e00\u4e2a\u4f7f\u7528\u5f62\u5f0f\u4e3a char *, int \u5f62\u5f0f\u7684\u5b57\u8282\uff0c\n\u800c\u53e6\u4e00\u4e2a\u4f7f\u7528\u5f62\u5f0f\u4e3a wchar_t *, int \u7684\u5bbd\u5b57\u7b26\u5f62\u5f0f\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "void print_chars(char *s, int len) {\n  int n = 0;\n\n  while (n < len) {\n    printf(\"%2x \", (unsigned char) s[n]);\n    n++;\n  }\n  printf(\"\\n\");\n}\n\nvoid print_wchars(wchar_t *s, int len) {\n  int n = 0;\n  while (n < len) {\n    printf(\"%x \", s[n]);\n    n++;\n  }\n  printf(\"\\n\");\n}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u9762\u5411\u5b57\u8282\u7684\u51fd\u6570 print_chars() \uff0c\u4f60\u9700\u8981\u5c06Python\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5408\u9002\u7684\u7f16\u7801\u6bd4\u5982UTF-8.\n\u4e0b\u9762\u662f\u4e00\u4e2a\u8fd9\u6837\u7684\u6269\u5c55\u51fd\u6570\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "static PyObject *py_print_chars(PyObject *self, PyObject *args) {\n  char *s;\n  Py_ssize_t  len;\n\n  if (!PyArg_ParseTuple(args, \"s#\", &s, &len)) {\n    return NULL;\n  }\n  print_chars(s, len);\n  Py_RETURN_NONE;\n}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u90a3\u4e9b\u9700\u8981\u5904\u7406\u673a\u5668\u672c\u5730 wchar_t \u7c7b\u578b\u7684\u5e93\u51fd\u6570\uff0c\u4f60\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u7f16\u5199\u6269\u5c55\u4ee3\u7801\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "static PyObject *py_print_wchars(PyObject *self, PyObject *args) {\n  wchar_t *s;\n  Py_ssize_t  len;\n\n  if (!PyArg_ParseTuple(args, \"u#\", &s, &len)) {\n    return NULL;\n  }\n  print_wchars(s,len);\n  Py_RETURN_NONE;\n}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u662f\u4e00\u4e2a\u4ea4\u4e92\u4f1a\u8bdd\u6765\u6f14\u793a\u8fd9\u4e2a\u51fd\u6570\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s = 'Spicy Jalape\\u00f1o'\nprint_chars(s)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print_wchars(s)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4ed4\u7ec6\u89c2\u5bdf\u8fd9\u4e2a\u9762\u5411\u5b57\u8282\u7684\u51fd\u6570 print_chars() \u662f\u600e\u6837\u63a5\u53d7UTF-8\u7f16\u7801\u6570\u636e\u7684\uff0c\n\u4ee5\u53ca print_wchars() \u662f\u600e\u6837\u63a5\u53d7Unicode\u7f16\u7801\u503c\u7684"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u7ee7\u7eed\u672c\u8282\u4e4b\u524d\uff0c\u4f60\u5e94\u8be5\u9996\u5148\u5b66\u4e60\u4f60\u8bbf\u95ee\u7684C\u51fd\u6570\u5e93\u7684\u7279\u5f81\u3002\n\u5bf9\u4e8e\u5f88\u591aC\u51fd\u6570\u5e93\uff0c\u901a\u5e38\u4f20\u9012\u5b57\u8282\u800c\u4e0d\u662f\u5b57\u7b26\u4e32\u4f1a\u6bd4\u8f83\u597d\u4e9b\u3002\u8981\u8fd9\u6837\u505a\uff0c\u8bf7\u4f7f\u7528\u5982\u4e0b\u7684\u8f6c\u6362\u4ee3\u7801\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "static PyObject *py_print_chars(PyObject *self, PyObject *args) {\n  char *s;\n  Py_ssize_t  len;\n\n  /* accepts bytes, bytearray, or other byte-like object */\n  if (!PyArg_ParseTuple(args, \"y#\", &s, &len)) {\n    return NULL;\n  }\n  print_chars(s, len);\n  Py_RETURN_NONE;\n}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u4ecd\u7136\u8fd8\u662f\u60f3\u8981\u4f20\u9012\u5b57\u7b26\u4e32\uff0c\n\u4f60\u9700\u8981\u77e5\u9053Python 3\u53ef\u4f7f\u7528\u4e00\u4e2a\u5408\u9002\u7684\u5b57\u7b26\u4e32\u8868\u793a\uff0c\n\u5b83\u5e76\u4e0d\u76f4\u63a5\u6620\u5c04\u5230\u4f7f\u7528\u6807\u51c6\u7c7b\u578b char * \u6216 wchar_t * \uff08\u66f4\u591a\u7ec6\u8282\u53c2\u8003PEP 393\uff09\u7684C\u51fd\u6570\u5e93\u3002\n\u56e0\u6b64\uff0c\u8981\u5728C\u4e2d\u8868\u793a\u8fd9\u4e2a\u5b57\u7b26\u4e32\u6570\u636e\uff0c\u4e00\u4e9b\u8f6c\u6362\u8fd8\u662f\u5fc5\u987b\u8981\u7684\u3002\n\u5728 PyArg_ParseTuple() \u4e2d\u4f7f\u7528\u201ds#\u201d \u548c\u201du#\u201d\u683c\u5f0f\u5316\u7801\u53ef\u4ee5\u5b89\u5168\u7684\u6267\u884c\u8fd9\u6837\u7684\u8f6c\u6362\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0d\u8fc7\u8fd9\u79cd\u8f6c\u6362\u6709\u4e2a\u7f3a\u70b9\u5c31\u662f\u5b83\u53ef\u80fd\u4f1a\u5bfc\u81f4\u539f\u59cb\u5b57\u7b26\u4e32\u5bf9\u8c61\u7684\u5c3a\u5bf8\u589e\u5927\u3002\n\u4e00\u65e6\u8f6c\u6362\u8fc7\u540e\uff0c\u4f1a\u6709\u4e00\u4e2a\u8f6c\u6362\u6570\u636e\u7684\u590d\u5236\u9644\u52a0\u5230\u539f\u59cb\u5b57\u7b26\u4e32\u5bf9\u8c61\u4e0a\u9762\uff0c\u4e4b\u540e\u53ef\u4ee5\u88ab\u91cd\u7528\u3002\n\u4f60\u53ef\u4ee5\u89c2\u5bdf\u4e0b\u8fd9\u79cd\u6548\u679c\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import sys\ns = 'Spicy Jalape\\u00f1o'\nsys.getsizeof(s)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print_chars(s)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "sys.getsizeof(s)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print_wchars(s)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "sys.getsizeof(s)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u5c11\u91cf\u7684\u5b57\u7b26\u4e32\u5bf9\u8c61\uff0c\u53ef\u80fd\u6ca1\u4ec0\u4e48\u5f71\u54cd\uff0c\n\u4f46\u662f\u5982\u679c\u4f60\u9700\u8981\u5728\u6269\u5c55\u4e2d\u5904\u7406\u5927\u91cf\u7684\u6587\u672c\uff0c\u4f60\u53ef\u80fd\u60f3\u907f\u514d\u8fd9\u4e2a\u635f\u8017\u4e86\u3002\n\u4e0b\u9762\u662f\u4e00\u4e2a\u4fee\u8ba2\u7248\u672c\u53ef\u4ee5\u907f\u514d\u8fd9\u79cd\u5185\u5b58\u635f\u8017\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "static PyObject *py_print_chars(PyObject *self, PyObject *args) {\n  PyObject *obj, *bytes;\n  char *s;\n  Py_ssize_t   len;\n\n  if (!PyArg_ParseTuple(args, \"U\", &obj)) {\n    return NULL;\n  }\n  bytes = PyUnicode_AsUTF8String(obj);\n  PyBytes_AsStringAndSize(bytes, &s, &len);\n  print_chars(s, len);\n  Py_DECREF(bytes);\n  Py_RETURN_NONE;\n}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u800c\u5bf9 wchar_t \u7684\u5904\u7406\u65f6\u60f3\u8981\u907f\u514d\u5185\u5b58\u635f\u8017\u5c31\u66f4\u52a0\u96be\u529e\u4e86\u3002\n\u5728\u5185\u90e8\uff0cPython\u4f7f\u7528\u6700\u9ad8\u6548\u7684\u8868\u793a\u6765\u5b58\u50a8\u5b57\u7b26\u4e32\u3002\n\u4f8b\u5982\uff0c\u53ea\u5305\u542bASCII\u7684\u5b57\u7b26\u4e32\u88ab\u5b58\u50a8\u4e3a\u5b57\u8282\u6570\u7ec4\uff0c\n\u800c\u5305\u542b\u8303\u56f4\u4eceU+0000\u5230U+FFFF\u7684\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\u4f7f\u7528\u53cc\u5b57\u8282\u8868\u793a\u3002\n\u7531\u4e8e\u5bf9\u4e8e\u6570\u636e\u7684\u8868\u793a\u5f62\u5f0f\u4e0d\u662f\u5355\u4e00\u7684\uff0c\u4f60\u4e0d\u80fd\u5c06\u5185\u90e8\u6570\u7ec4\u8f6c\u6362\u4e3a wchar_t * \u7136\u540e\u671f\u671b\u5b83\u80fd\u6b63\u786e\u7684\u5de5\u4f5c\u3002\n\u4f60\u5e94\u8be5\u521b\u5efa\u4e00\u4e2a wchar_t \u6570\u7ec4\u5e76\u5411\u5176\u4e2d\u590d\u5236\u6587\u672c\u3002\nPyArg_ParseTuple() \u7684\u201du#\u201d\u683c\u5f0f\u7801\u53ef\u4ee5\u5e2e\u52a9\u4f60\u9ad8\u6548\u7684\u5b8c\u6210\u5b83\uff08\u5b83\u5c06\u590d\u5236\u7ed3\u679c\u9644\u52a0\u5230\u5b57\u7b26\u4e32\u5bf9\u8c61\u4e0a\uff09\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u60f3\u907f\u514d\u957f\u65f6\u95f4\u5185\u5b58\u635f\u8017\uff0c\u4f60\u552f\u4e00\u7684\u9009\u62e9\u5c31\u662f\u590d\u5236Unicode\u6570\u636e\u61c2\u554a\u4e00\u4e2a\u4e34\u65f6\u7684\u6570\u7ec4\uff0c\n\u5c06\u5b83\u4f20\u9012\u7ed9C\u51fd\u6570\uff0c\u7136\u540e\u56de\u6536\u8fd9\u4e2a\u6570\u7ec4\u7684\u5185\u5b58\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u53ef\u80fd\u7684\u5b9e\u73b0\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "static PyObject *py_print_wchars(PyObject *self, PyObject *args) {\n  PyObject *obj;\n  wchar_t *s;\n  Py_ssize_t len;\n\n  if (!PyArg_ParseTuple(args, \"U\", &obj)) {\n    return NULL;\n  }\n  if ((s = PyUnicode_AsWideCharString(obj, &len)) == NULL) {\n    return NULL;\n  }\n  print_wchars(s, len);\n  PyMem_Free(s);\n  Py_RETURN_NONE;\n}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8fd9\u4e2a\u5b9e\u73b0\u4e2d\uff0cPyUnicode_AsWideCharString() \u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u7684wchar_t\u7f13\u51b2\u5e76\u590d\u5236\u6570\u636e\u8fdb\u53bb\u3002\n\u8fd9\u4e2a\u7f13\u51b2\u88ab\u4f20\u9012\u7ed9C\u7136\u540e\u88ab\u91ca\u653e\u6389\u3002\n\u4f46\u662f\u6211\u5199\u8fd9\u672c\u4e66\u7684\u65f6\u5019\uff0c\u8fd9\u91cc\u53ef\u80fd\u6709\u4e2abug\uff0c\u540e\u9762\u7684Python\u95ee\u9898\u9875\u6709\u4ecb\u7ecd\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u77e5\u9053C\u51fd\u6570\u5e93\u9700\u8981\u7684\u5b57\u8282\u7f16\u7801\u5e76\u4e0d\u662fUTF-8\uff0c\n\u4f60\u53ef\u4ee5\u5f3a\u5236Python\u4f7f\u7528\u6269\u5c55\u7801\u6765\u6267\u884c\u6b63\u786e\u7684\u8f6c\u6362\uff0c\u5c31\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "static PyObject *py_print_chars(PyObject *self, PyObject *args) {\n  char *s = 0;\n  int   len;\n  if (!PyArg_ParseTuple(args, \"es#\", \"encoding-name\", &s, &len)) {\n    return NULL;\n  }\n  print_chars(s, len);\n  PyMem_Free(s);\n  Py_RETURN_NONE;\n}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\uff0c\u5982\u679c\u4f60\u60f3\u76f4\u63a5\u5904\u7406Unicode\u5b57\u7b26\u4e32\uff0c\u4e0b\u9762\u7684\u662f\u4f8b\u5b50\uff0c\u6f14\u793a\u4e86\u5e95\u5c42\u64cd\u4f5c\u8bbf\u95ee\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "static PyObject *py_print_wchars(PyObject *self, PyObject *args) {\n  PyObject *obj;\n  int n, len;\n  int kind;\n  void *data;\n\n  if (!PyArg_ParseTuple(args, \"U\", &obj)) {\n    return NULL;\n  }\n  if (PyUnicode_READY(obj) < 0) {\n    return NULL;\n  }\n\n  len = PyUnicode_GET_LENGTH(obj);\n  kind = PyUnicode_KIND(obj);\n  data = PyUnicode_DATA(obj);\n\n  for (n = 0; n < len; n++) {\n    Py_UCS4 ch = PyUnicode_READ(kind, data, n);\n    printf(\"%x \", ch);\n  }\n  printf(\"\\n\");\n  Py_RETURN_NONE;\n}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8fd9\u4e2a\u4ee3\u7801\u4e2d\uff0cPyUnicode_KIND() \u548c PyUnicode_DATA()\n\u8fd9\u4e24\u4e2a\u5b8f\u548cUnicode\u7684\u53ef\u53d8\u5bbd\u5ea6\u5b58\u50a8\u6709\u5173\uff0c\u8fd9\u4e2a\u5728PEP 393\u4e2d\u6709\u63cf\u8ff0\u3002\nkind \u53d8\u91cf\u7f16\u7801\u5e95\u5c42\u5b58\u50a8\uff088\u4f4d\u300116\u4f4d\u621632\u4f4d\uff09\u4ee5\u53ca\u6307\u5411\u7f13\u5b58\u7684\u6570\u636e\u6307\u9488\u76f8\u5173\u7684\u4fe1\u606f\u3002\n\u5728\u5b9e\u9645\u60c5\u51b5\u4e2d\uff0c\u4f60\u5e76\u4e0d\u9700\u8981\u77e5\u9053\u4efb\u4f55\u8ddf\u8fd9\u4e9b\u503c\u6709\u5173\u7684\u4e1c\u897f\uff0c\n\u53ea\u9700\u8981\u5728\u63d0\u53d6\u5b57\u7b26\u7684\u65f6\u5019\u5c06\u5b83\u4eec\u4f20\u7ed9 PyUnicode_READ() \u5b8f\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd8\u6709\u6700\u540e\u51e0\u53e5\uff1a\u5f53\u4ecePython\u4f20\u9012Unicode\u5b57\u7b26\u4e32\u7ed9C\u7684\u65f6\u5019\uff0c\u4f60\u5e94\u8be5\u5c3d\u91cf\u7b80\u5355\u70b9\u3002\n\u5982\u679c\u6709UTF-8\u548c\u5bbd\u5b57\u7b26\u4e24\u79cd\u9009\u62e9\uff0c\u8bf7\u9009\u62e9UTF-8.\n\u5bf9UTF-8\u7684\u652f\u6301\u66f4\u52a0\u666e\u904d\u4e00\u4e9b\uff0c\u4e5f\u4e0d\u5bb9\u6613\u72af\u9519\uff0c\u89e3\u91ca\u5668\u4e5f\u80fd\u652f\u6301\u7684\u66f4\u597d\u4e9b\u3002\n\u6700\u540e\uff0c\u786e\u4fdd\u4f60\u4ed4\u7ec6\u9605\u8bfb\u4e86 \u5173\u4e8e\u5904\u7406Unicode\u7684\u76f8\u5173\u6587\u6863"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}